//
//  CQWelcomeViewController.swift
//  swift-weibo
//
//  Created by 陈乾 on 15/10/29.
//  Copyright © 2015年 陈乾. All rights reserved.
//

import UIKit
import SDWebImage

class CQWelcomeViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        //如果有值才去网络请求头像
        if let urlStr = CQUserAccount.loadAccount()?.avatar_large {
         
            iconView.sd_setImageWithURL(NSURL(string: urlStr), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
    }
    
   private var iconViewConstraint:NSLayoutConstraint?
    
    //MARK: - 动画
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        iconViewConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
             //控制器的view重新布局一下
             self.view.layoutIfNeeded()
            }) { (_) -> Void in
                //完成
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.welcomeTitle.alpha = 1
                    }, completion: { (_) -> Void in
                        //完成就跳转到首页控制器
                        (UIApplication.sharedApplication().delegate as? AppDelegate)?.switchVC(true)
                })
        }
        
 
    }
    
   
    
    
    //MARK: - 准备UI
    func prepareUI() {
        
        //添加子控件
        view.addSubview(backgroundView)
        view.addSubview(iconView)
        view.addSubview(welcomeTitle)
        
        //设置前需要把约束干预去掉
        backgroundView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeTitle.translatesAutoresizingMaskIntoConstraints = false
        
        //背景设置约束
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bkg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkg" : backgroundView]))
        
       view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bkg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkg" : backgroundView]))
        //设置icomView的约束
        //水平居中
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //距离底部160
        iconViewConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(iconViewConstraint!)
        //宽高
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        //文字
        //居中
        view.addConstraint(NSLayoutConstraint(item: welcomeTitle, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: welcomeTitle, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
     
    }
    
    //MARK: - 懒加载
    //懒加载背景view
    lazy var backgroundView: UIImageView = {
        //创建并设置图
        let backView = UIImageView(image: UIImage(named: "ad_background"))
        return backView
        }()
    //头像
    lazy var iconView: UIImageView = {
        
        let icView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //设置圆形头像
        icView.layer.cornerRadius = 42.5
        icView.layer.masksToBounds = true
        return icView
        }()
    //文字
    lazy var welcomeTitle: UILabel = {
        let label = UILabel()
        label.text = "欢迎来到英雄联盟"
        //透明
        label.alpha = 0
        //设置大小
        label.sizeToFit()
        return label
        }()
    
/********end**********/
}
